import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:riverpod_t/page/home/data/model/cartoon_model_entity.dart';
import 'package:riverpod_t/widget/app_net_image.dart';

class CarTonWidget extends StatelessWidget {
  final int index;
  final CartoonModelDataFeeds model;
  final bool? isStaggeredGrid;
  final VoidCallback onTap; // 使用 VoidCallback 替代 Function

  const CarTonWidget({
    super.key,
    required this.index,
    required this.model,
    this.isStaggeredGrid,
    required this.onTap,
  });

  /// 计算描述文本
  String get _descriptionText {
    final tags = model.comicInfo?.tags ?? [];
    final mainStyleName = model.comicInfo?.mainStyleName ?? '';

    if (tags.isNotEmpty) {
      return mainStyleName.isNotEmpty ? "$mainStyleName | ${tags.first}" : tags.first;
    }
    return mainStyleName.isNotEmpty ? mainStyleName : "暂无描述";
  }

  /// 图片部件
  Widget get _imageWidget => AppNetImage(
    radius: 6.h,
    fit: BoxFit.fill,
    width: (1.sw - 30.w) / 2,
    height: 260.h,
    imageUrl: model.image ?? '',
  );

  /// 标题部件
  Widget get _titleWidget => Padding(
    padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 10.h, bottom: 6.h),
    child: Text(
      model.title ?? '',
      overflow: TextOverflow.ellipsis,
      maxLines: 1, // 添加最大行数限制
      style: TextStyle(color: Colors.black, fontWeight: FontWeight.w500, fontSize: 16.sp),
    ),
  );

  /// 描述部件
  Widget get _desWidget => Padding(
    padding: EdgeInsets.only(left: 10.w, right: 10.w),
    child: Text(
      _descriptionText,
      overflow: TextOverflow.ellipsis,
      maxLines: 1, // 添加最大行数限制
      style: TextStyle(color: Colors.black45, fontWeight: FontWeight.w500, fontSize: 14.sp),
    ),
  );

  /// 点赞
  Widget get _likeWidget => Padding(
    padding: EdgeInsets.symmetric(horizontal: 6.w),
    child: Image.asset(
      "assets/images/zan_nonal.png",
      width: model.isLike ? 16.w : 16.w,
      height: model.isLike ? 16.w : 16.w,
      color: model.isLike ? Colors.red : Colors.grey,
    ),
  );

  /// 上方信息容器
  Widget get _infoContainer => Container(
    width: (1.sw - 30.w) / 2,
    padding: EdgeInsets.only(bottom: 18.h),
    decoration: BoxDecoration(
      color: Colors.white,
      border: Border.all(width: 1.w, color: Colors.black12),
      borderRadius: BorderRadius.vertical(bottom: Radius.circular(6.h)),
    ),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min, // 优化布局
      children: [
        _titleWidget,
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [Expanded(child: _desWidget), _likeWidget],
        ),
      ],
    ),
  );

  /// 获取水平边距
  EdgeInsets get _horizontalPadding {
    if (isStaggeredGrid != true) return EdgeInsets.zero;
    return EdgeInsets.only(left: index % 3 == 1 ? 10.w : 5.w, right: index % 3 == 2 ? 10.w : 5.w);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap, // 直接使用 onTap，无需额外包装
      child: Container(
        padding: _horizontalPadding,
        decoration: BoxDecoration(
          color: Color(0xFFF3F4F8),
          borderRadius: BorderRadius.all(Radius.circular(6.h)),
        ),
        alignment: Alignment.center,
        child: Stack(
          clipBehavior: Clip.none, // 允许子部件溢出
          children: [_imageWidget, Positioned(bottom: 0, left: 0, child: _infoContainer)],
        ),
      ),
    );
  }
}
